// src/utils/cookie.js

/**
 * 设置带过期时间的 Cookie
 * @param {string} name - Cookie 名称
 * @param {string} value - Cookie 值
 * @param {number} days - 过期天数
 */
export const setCookie = (name, value, days) => {
  const expires = new Date()
  expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000)
  // expires.setDate(expires.getTime() + days * 1000 * 10)
  document.cookie = `${name}=${value};expires=${expires.toUTCString()};path=/`
}

/**
 * 获取 Cookie 值
 * @param {string} name - Cookie 名称
 * @returns {string|null} Cookie 值或 null
 */
export const getCookie = (name) => {
  const nameEQ = name + '='
  const ca = document.cookie.split(';')
  for (let i = 0; i < ca.length; i++) {
    let c = ca[i]
    while (c.charAt(0) === ' ') c = c.substring(1, c.length)
    if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length)
  }
  return null
}
